mc.game.task = function(){};
mc.game.task.set_available = function(data){
	var h = {};
	var proto = mc.game.get_proto("task_types");
	for(var i = 0; i < data.length; i++){
		var cur = data[i];
		var t = "leader_" + cur.leader_type;
		if(!h[t]){
			h[t] = [];
		}
		var type = cur.type;
		cur.type = proto[type];
		h[t].push(cur);
	}
	mc.game.task._by_type = h;
}
mc.game.task.by_type = function(type){
	var t = "leader_" + type;
	return mc.game.task._by_type[t];
}
mc.game.task.ask_available = function(){
	if(mc.game.task._by_type){
		return null;
	}else{
		return mc.request(
				"game",
				{cmd: "task.load"},
				function(data){
					mc.game.task.set_available(data);
				},
				{}
			);
	}
}


mc.game.task.show = function(){
	mc.game.army.show_for(mc.game.task);
}

mc.game.task.leader_click = function(event){
	var entry = mc.game.army.leader_select(this);
	var to_current = mc.cmd.get_right_inner(1).empty();
	var to_available = mc.cmd.get_right_inner(2).empty();
	
	mc.game.load_start();
	$.when(
		mc.leader_list.ask_task(entry),
		mc.game.task.ask_available(entry)
	).then(function(){
		mc.game.task.draw_current(entry,to_current);
		mc.game.task.draw_available(entry,to_available);
		mc.game.load_end();
	});
	
	if(event){
		event.stopImmediatePropagation();
	}
}
mc.game.task.draw_current = function(entry,to){
	var task = mc.leader_list.task(entry);
	if(!task){
		return;
	}
	$.tmpl("active_task",task).appendTo(to);
}
mc.game.task.draw_available = function(entry,to){
	var h = mc.game.calc_heigth();
	var leader = mc.leader_list.leader(entry);
	var list = mc.game.task.by_type(leader.proto_id);
	var o = $.tmpl("available_task",{list:list}).appendTo(to);
	o.height(h);
	o.jScrollPane(scroll_pane);
}
